﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using DRHotel_v1.Classes;
using DRHotel_v1.Classes.DBManagers;

namespace DRHotel_v1.HousekeepingAndStaffMngmt
{
    public partial class ItemManagement : Form
    {
        public ItemManagement()
        {
            InitializeComponent();

            //Setting up comboboxes
            cbxSearchBy.SelectedIndex = 0;
            cbxSearchBy2.SelectedIndex = 0;
        }

        private void btnCreateItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure you want to create this item?", "Confirmation Message", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                // a 'DialogResult.Yes' value was returned from the MessageBox
                // proceed with your deletion


                #region declare item
                //Declaring variables
                string itemName = "";
                double itemPrice = 0;

                //Declaring item variables
                itemName = tbxItemName.Text;
                itemPrice = Convert.ToDouble(tbxItemPrice.Text);

                //Input item data
                Item item = new Item();
                item.itemName = itemName;
                item.itemPrice = itemPrice;
                #endregion

                #region check existing item & insert item 
                //Check for existing item
                ArrayList allItem = HousekeepingDBManager.GetAllItemList();
                bool insert = true;
                bool success = false;
                foreach (Item item2 in allItem)
                {
                    if (item2.itemName.Equals(item.itemName))
                    {
                        MessageBox.Show("Item already exists!");
                        insert = false;
                        break;
                    }
                }

                //If there isnt any existing item, insert item
                if (insert == true)
                {
                    success = HousekeepingDBManager.InsertItem(item);
                    if (success == true)
                    {
                        MessageBox.Show("Item created successfully!");

                    }
                    else
                    {
                        MessageBox.Show("Item not created, please try again!");
                    }
                }
                #endregion
            }
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string value = Convert.ToString(cbxSearch.SelectedItem);
            string type = cbxSearchBy.SelectedItem.ToString();

            ItemManagementRetrievedFeedback form = new ItemManagementRetrievedFeedback(type, value);
            form.ShowDialog();
        }

        private void btnRetrieveAll_Click(object sender, EventArgs e)
        {
            string type = "All";
            string value = "All";

            ItemManagementRetrievedFeedback form = new ItemManagementRetrievedFeedback(type, value);
            form.ShowDialog();
        }

        private void cbxSearchBy_SelectedIndexChanged(object sender, EventArgs e)
        {
            string searchBy = cbxSearchBy.SelectedItem.ToString();

            //Defining what the user search by
            if (searchBy.Equals("Item ID"))
            {
                ArrayList itemList = HousekeepingDBManager.GetItemList();
                cbxSearch.Items.Clear();

                for (int i = 0; i < itemList.Count; i++)
                {
                    Item item2 = (Item)itemList[i];
                    cbxSearch.Items.Add(item2.itemID);
                }
            }
            else if (searchBy.Equals("Item Name"))
            {
                ArrayList itemList = HousekeepingDBManager.GetItemList();
                cbxSearch.Items.Clear();

                for (int i = 0; i < itemList.Count; i++)
                {
                    Item item2 = (Item)itemList[i];
                    cbxSearch.Items.Add(item2.itemName);
                }
            }
        }

        private void cbxSearchBy2_SelectedIndexChanged(object sender, EventArgs e)
        {
            string searchBy2 = cbxSearchBy2.SelectedItem.ToString();

            //Defining what the user search by
            if (searchBy2.Equals("Item ID"))
            {
                ArrayList itemList = HousekeepingDBManager.GetItemList();
                cbxSearch2.Items.Clear();

                for (int i = 0; i < itemList.Count; i++)
                {
                    Item item2 = (Item)itemList[i];
                    cbxSearch2.Items.Add(item2.itemID);
                }
            }
            else if (searchBy2.Equals("Item Name"))
            {
                ArrayList itemList = HousekeepingDBManager.GetItemList();
                cbxSearch2.Items.Clear();

                for (int i = 0; i < itemList.Count; i++)
                {
                    Item item2 = (Item)itemList[i];
                    cbxSearch2.Items.Add(item2.itemName);
                }
            }
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            string value = Convert.ToString(cbxSearch2.SelectedItem);
            string type = cbxSearchBy2.SelectedItem.ToString();

            ItemManagementEdit form = new ItemManagementEdit(type, value);
            form.ShowDialog();
        }

        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        // Validation of user input
        //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
        private void tbxItemName_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(tbxItemName.Text))
            {
                MessageBox.Show("Item name required!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxItemName.SelectAll();
                tbxItemName.Focus();
                return;
            }
        }
        private void tbxItemPrice_Validating(object sender, CancelEventArgs e)
        {
            if (string.IsNullOrEmpty(tbxItemPrice.Text))
            {
                MessageBox.Show("Item price required!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxItemPrice.SelectAll();
                tbxItemPrice.Focus();
                return;
            }
            else if (!Regex.IsMatch(tbxItemPrice.Text, @"^[0-9]+$"))
            {
                MessageBox.Show("Item price invalid!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
                tbxItemPrice.SelectAll();
                tbxItemPrice.Focus();
                return;
            }
        }
    }
}
